package com.aaa.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.entity.logMonitor.Log;
import com.aaa.entity.systemmanage.User;
import com.aaa.service.logMonitor.LogService;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;

/**
 * @author ：XiaoXuan
 * @date ：Created in 2022/7/2 9:13
 * @description：日志切面类
 * @modified By：
 * @version: 1.0
 */
@Component
@Aspect
@EnableAspectJAutoProxy
@lombok.extern.java.Log
public class LogAspect {

    @Pointcut("@annotation(com.aaa.aspect.LogMonitor)")
    public void zzz(){}

    @Autowired
    private LogService logService;
    /**
     * @param joinPoint:
     * @return Object
     * @author W
     * @description 添加日志
     * @date 2022/7/8 8:53
     */
    @Before("zzz()")
    public void myEnhance(JoinPoint joinPoint) {
        System.out.println("2222222222222222222222222");
        //获取方法的参数
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        //保存日志信息到数据库中
        Log log = new Log();
        //获取请求的ip地址
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        log.setIp(ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip);
        HttpSession session = request.getSession();
        //获取类名
        String className = joinPoint.getTarget().getClass().getName();
        log.setClassName(className);
        //获取方法名
        log.setMethodName(signature.getName());
        //获取参数
        String paramString = Arrays.toString(args);
        log.setParams(paramString);
        if (paramString.getBytes().length > 8889) {
            log.setParams("Too Long Params To Save ");
        }
        //设置时间
        log.setCreateTime(new Date());
        if (ObjectUtil.isNotEmpty(session.getAttribute("user"))) {
            //if (signature.getName().equals("queryAll")){
            //}else {
                User user = (User) session.getAttribute("user");
                log.setUserName( user.getUsername());
                logService.insert(log);
            //}
        }

    }
}
